package framework.mvc;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public abstract class AFincoView extends javax.swing.JFrame {

    private JTable table;
    private DefaultTableModel viewModel;
    private int selectedRowIndex;

    public AFincoView() {
        JPanel mainPanel = new JPanel();

        setTitle(getAppTitle());
        setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout(0, 0));
        setSize(575, 310);
        setVisible(false);
        mainPanel.setLayout(null);
        getContentPane().add(BorderLayout.CENTER, mainPanel);
        mainPanel.setBounds(0, 0, 575, 310);

        JScrollPane scrollPane = new JScrollPane();
        mainPanel.add(scrollPane);

        viewModel = new DefaultTableModel();
        table = new JTable(viewModel);

        scrollPane.setBounds(12, 92, 444, 160);
        scrollPane.getViewport().add(table);
        table.setBounds(0, 0, 420, 0);

        for (String header : getTableHeaders())
            viewModel.addColumn(header);


        Box horizontalBox = Box.createHorizontalBox();
        mainPanel.add(horizontalBox);
        horizontalBox.setBounds(24, 20, 445, 35);
        addButtons(horizontalBox);

        Box verticalBox = Box.createVerticalBox();
        mainPanel.add(verticalBox);
        verticalBox.setBounds(468, 104, 96, 120);
        verticalBox.add(new CreditButton(this, getCreditReifiedName()).getButton());
        verticalBox.add(new DebtButton(this, getDebtReifiedName()).getButton());
        verticalBox.add(new ExitButton(this, "Exit").getButton());
        this.addWindowListener(new SymWindow());

    }

    protected abstract String getAppTitle();

    protected abstract String getCreditReifiedName();

    protected abstract String getDebtReifiedName();

    protected abstract String[] getTableHeaders();

    protected abstract void addButtons(Box box);


    public DefaultTableModel getViewModel() {
        return viewModel;
    }

    public int getSelectedRowIndex() {
        return selectedRowIndex;
    }

    public void setSelectedRowIndex(int selectedRowIndex) {
        this.selectedRowIndex = selectedRowIndex;
    }

    private class SymWindow extends java.awt.event.WindowAdapter {
        public void windowClosing(java.awt.event.WindowEvent event) {
            setVisible(false); // hide the Frame
            dispose(); // free the system resources
            System.exit(0); // close the application
        }
    }

    public void unselectRow() {
        table.getSelectionModel().setAnchorSelectionIndex(-1);
    }

    public int getRowSelectedIndex() {
        return table.getSelectionModel().getMinSelectionIndex();
    }

}